<?php
/**
 * Created by : PhpStorm
 * Index: 阿城 <479468212@qq.com>
 * Date: 2019/12/27
 * Time: 13:02
 */

namespace app\api\model;

use app\common\model\Base;
use think\cache\driver\Redis;
use think\facade\Config;

class User extends Base {
    protected $autoWriteTimestamp = true;
    public static $realStatusArr;
    // 追加属性
    protected $append = ['real_status_text','preview_idcard_front_url','preview_idcard_back_url','preview_idcard_hand_url'];

    // 模型初始化
    protected static function init()
    {
        self::$realStatusArr = get_data_dict('user_real_status');
    }

    public function userData() {
        return $this->hasOne('UserData', 'uid', 'id');
    }

    /**
     * 更新用户信息
     */
    public static function updateRedisUserInfo($uid)
    {
        $field = 'id,username,nickname,type,status,phone,real_status,realname,idcard_img_front,idcard_img_back,idcard_img_hand';
        $userInfo = self::field($field)->where(['id' => $uid])->find();
        $userInfo->userData;

        Config::load(base_path().'/api/config/cache.php', 'apiCacheConfig');
        $apiCachePrefix = Config::get('apiCacheConfig.stores.redis.prefix');

        $handler = new Redis();
        $expire = config('easyApi.ONLINE_TIME');
        $token = $handler->get($apiCachePrefix.'Login:'. $uid);
        if(is_serialized_string($token)){
            $token = unserialize($token);
        }
        $handler->setex($apiCachePrefix.'Login:' . $token, $expire, serialize($userInfo->toArray()));
        $handler->setex($apiCachePrefix.'Login:' . $uid, $expire, serialize($token));
        return true;
    }

    /**
     * 删除用户信息
     */
    public static function deleteRedisUserInfo($uid)
    {
        Config::load(base_path().'/api/config/cache.php', 'apiCacheConfig');
        $apiCachePrefix = Config::get('apiCacheConfig.stores.redis.prefix');

        $handler = new Redis();
        $token = unserialize($handler->get($apiCachePrefix.'Login:'. $uid));
        if(is_serialized_string($token)){
            $token = unserialize($token);
        }
        $handler->delete($apiCachePrefix.'Login:' . $token);
        $handler->delete($apiCachePrefix.'Login:' . $uid);
        return true;
    }

    public function getRealStatusTextAttr($value,$data)
    {
        if(strlen($data['real_status']) <= 0){
            return '';
        }
        return self::$realStatusArr[$data['real_status']];
    }

    public function getPreviewIdcardFrontUrlAttr($value,$data)
    {
        if(empty($data['idcard_img_front'])){
            return '';
        }
        $filesystemConfig = config('filesystem.disks.public');
        return request()->domain().$filesystemConfig['url'].$data['idcard_img_front'];
    }

    public function getPreviewIdcardBackUrlAttr($value,$data)
    {
        if(empty($data['idcard_img_back'])){
            return '';
        }
        $filesystemConfig = config('filesystem.disks.public');
        return request()->domain().$filesystemConfig['url'].$data['idcard_img_back'];
    }

    public function getPreviewIdcardHandUrlAttr($value,$data)
    {
        if(empty($data['idcard_img_hand'])){
            return '';
        }
        $filesystemConfig = config('filesystem.disks.public');
        return request()->domain().$filesystemConfig['url'].$data['idcard_img_hand'];
    }

}
